const nameInp = document.querySelector('.username')
const pwdInp = document.querySelector('.password')
const rpwdInp = document.querySelector('.rpassword')
const btn = document.querySelector('.submit>button')
const mustBox = document.querySelector('.must')
const mustBox2 = document.querySelector('.must2')
// 1. 绑定点击事件
btn.onclick = e => {
  try{ e.preventDefault()}catch(err){
    e.returnValue = false
  }
  // 2. 拿到用户输入的内容
  const name = nameInp.value
  const pwd = pwdInp.value
  const rpwd = rpwdInp.value

  // 3. 非空验证
  if (!name || !pwd || !rpwd) return alert('请完整填写表单！')
  if( pwd !== rpwd ) return alert('两次输入的密码不一样！')
  // 4. 发送请求
  const data = `username=${ name }&password=${ pwd }&rpassword=${ rpwd }`
  const xhr = new XMLHttpRequest()
  xhr.open('POST', '../../server/regist.php')
  xhr.onload = function () {
    const res = JSON.parse(xhr.responseText)
    console.log(res)

    if(res.code === 1 ) return window.location.href = './login.html'
    if(res.code === 2 ){
      mustBox2.style.display = 'block'
    }
  }
  xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
  xhr.send(data)
}